<?php

namespace app\wx\controller;

use think\Db;

class User extends CommonController
{
    const ROLE_COMPANY = 1;         // 保险公司人员
    const ROLE_CODER = 2;           // 代录人
    const ROLE_PROTECT = 3;         // 被保险人
    const ROLE_SALE_MAN = 4;        // 业务员

    public $request;

    public function __construct(\think\Request $request)
    {
        parent::__construct($request);
        $this->request = $request->param();
    }


    /**
     * 所有角色查询个人信息（通用）
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function userInfo()
    {
        $user_id = $this->uid;
        $res = Db::table('user_info')->where('ui_id',$user_id)->find();
        switch ($res['role_id']){
            case self::ROLE_COMPANY:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
            case self::ROLE_CODER:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                $a = Db::table('user_code_r')->where('recoder_id',$user_id)->find();
                $user = Db::table('user_info')->where('ui_id',$a['user_id'])->find();
                $data['company_man'] = $user['username'];
                $data['company_linkman'] = $user['mobile'];
                break;
            case self::ROLE_PROTECT:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
            case self::ROLE_SALE_MAN:
                $field = [
                    'user_info.head_img',
                    'user_info.role_id',
                    'user_info.username',
                    'user_info.mobile',
                    'user_info.company_name',
                    'user_info.card_num',
                    'user_info.sale_man_company',
                    'user_info.sale_man_linkman',
                ];
                $data = Db::table('user_info')->where('ui_id',$user_id)->field($field)->find();
                break;
        }
        $this->assign('info',$data);

        return $this->fetch('user/userInfo');
    }

    /**
     * 个人中心
     * @return \think\response\View
     */
    public function person()
    {
        $info = Db::table('user_info')->where('ui_id',$this->uid)->find();
        $this->assign('info',$info);
        return view();
    }


    public function change_head_img()
    {
        $head_img = $this->request['head_img'];
        $data = [
            'head_img'=>$head_img,
            'updated_at'=>date('Y-m-d H:i:s',time()),
        ];
        $res = Db::table('user_info')->where('ui_id',$this->uid)->update($data);
        if($res){
            return $this->response->json(['data'=>[],'code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }else{
            return $this->response->json(['data'=>[],'code'=>self::ERROR_CODE,'msg'=>'fail']);
        }
    }

}
